using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Text;

namespace Data_Access_Tier
{
	public class SYS_COMPANYData : DatabaseObjectBase
	{
		public SYS_COMPANYData()
		{
			Initialize(true);
		}
		public SYS_COMPANYData(Boolean newConnection)
		{
			Initialize(newConnection);
		}

		public Int32 SYS_COMPANY_Insert(String Company_Id, String CompanyCode, String Company, String Address, String Tel, String Fax, String WebSite, String Email, String Tax, String Licence, String Photo, String BankAccount, String OpenedAt)
		{
			try
			{
				if (_dbconn.State != ConnectionState.Open)
					_dbconn.Open();
				_dbadapter.InsertCommand = new SqlCommand("SYS_COMPANY_Insert");
				_dbadapter.InsertCommand.CommandType = CommandType.StoredProcedure;
				_dbadapter.InsertCommand.Connection = _dbconn;
				_trans = _dbconn.BeginTransaction();
				_dbadapter.InsertCommand.Transaction = _trans;
				_dbadapter.InsertCommand.Parameters.Clear();
				_dbadapter.InsertCommand.Parameters.Add(new SqlParameter("@Company_Id", SqlDbType.VarChar, 20));
				_dbadapter.InsertCommand.Parameters.Add(new SqlParameter("@CompanyCode", SqlDbType.VarChar, 250));
				_dbadapter.InsertCommand.Parameters.Add(new SqlParameter("@Company", SqlDbType.NVarChar, 250));
				_dbadapter.InsertCommand.Parameters.Add(new SqlParameter("@Address", SqlDbType.NVarChar, 250));
				_dbadapter.InsertCommand.Parameters.Add(new SqlParameter("@Tel", SqlDbType.VarChar, 50));
				_dbadapter.InsertCommand.Parameters.Add(new SqlParameter("@Fax", SqlDbType.VarChar, 50));
				_dbadapter.InsertCommand.Parameters.Add(new SqlParameter("@WebSite", SqlDbType.VarChar, 250));
				_dbadapter.InsertCommand.Parameters.Add(new SqlParameter("@Email", SqlDbType.VarChar, 50));
				_dbadapter.InsertCommand.Parameters.Add(new SqlParameter("@Tax", SqlDbType.VarChar, 50));
				_dbadapter.InsertCommand.Parameters.Add(new SqlParameter("@Licence", SqlDbType.NVarChar, 50));
				_dbadapter.InsertCommand.Parameters.Add(new SqlParameter("@Photo", SqlDbType.Image));
				_dbadapter.InsertCommand.Parameters.Add(new SqlParameter("@BankAccount", SqlDbType.VarChar, 20));
				_dbadapter.InsertCommand.Parameters.Add(new SqlParameter("@OpenedAt", SqlDbType.NVarChar, 255));
				_dbadapter.InsertCommand.Prepare();
				SqlString Company_IdSQL = Company_Id;
				_dbadapter.InsertCommand.Parameters["@Company_Id"].Value = Company_IdSQL;
				SqlString CompanyCodeSQL = CompanyCode;
				_dbadapter.InsertCommand.Parameters["@CompanyCode"].Value = CompanyCodeSQL;
				SqlString CompanySQL = Company;
				_dbadapter.InsertCommand.Parameters["@Company"].Value = CompanySQL;
				SqlString AddressSQL = Address;
				_dbadapter.InsertCommand.Parameters["@Address"].Value = AddressSQL;
				SqlString TelSQL = Tel;
				_dbadapter.InsertCommand.Parameters["@Tel"].Value = TelSQL;
				SqlString FaxSQL = Fax;
				_dbadapter.InsertCommand.Parameters["@Fax"].Value = FaxSQL;
				SqlString WebSiteSQL = WebSite;
				_dbadapter.InsertCommand.Parameters["@WebSite"].Value = WebSiteSQL;
				SqlString EmailSQL = Email;
				_dbadapter.InsertCommand.Parameters["@Email"].Value = EmailSQL;
				SqlString TaxSQL = Tax;
				_dbadapter.InsertCommand.Parameters["@Tax"].Value = TaxSQL;
				SqlString LicenceSQL = Licence;
				_dbadapter.InsertCommand.Parameters["@Licence"].Value = LicenceSQL;
				SqlBinary PhotoSQL = Convert.FromBase64String(Photo);
				_dbadapter.InsertCommand.Parameters["@Photo"].Value = PhotoSQL;
				SqlString BankAccountSQL = BankAccount;
				_dbadapter.InsertCommand.Parameters["@BankAccount"].Value = BankAccountSQL;
				SqlString OpenedAtSQL = OpenedAt;
				_dbadapter.InsertCommand.Parameters["@OpenedAt"].Value = OpenedAtSQL;
				Int32 returnvalue = Convert.ToInt32(_dbadapter.InsertCommand.ExecuteScalar());

				_trans.Commit();
				return returnvalue;
			}
			catch (SqlException e)
			{
				try
				{
					_trans.Rollback();
				}
				catch (SqlException ex)
				{
					throw new Exception("SQL Rollback Error: SYS_COMPANY_Insert: " + ex.Message, ex);
				}
				throw new Exception("SQL Error: SYS_COMPANY_Insert: " + e.Message, e);
			}
			finally
			{
				if (_dbadapter.InsertCommand != null)
					_dbadapter.InsertCommand.Dispose();
				CloseConnection();
			}
		}
		public Int32 SYS_COMPANY_Update(String Company_Id, String CompanyCode, String Company, String Address, String Tel, String Fax, String WebSite, String Email, String Tax, String Licence, String Photo, String BankAccount, String OpenedAt)
		{
			try
			{
				if (_dbconn.State != ConnectionState.Open)
					_dbconn.Open();
				_dbadapter.UpdateCommand = new SqlCommand("SYS_COMPANY_Update");
				_dbadapter.UpdateCommand.CommandType = CommandType.StoredProcedure;
				_dbadapter.UpdateCommand.Connection = _dbconn;

				_trans = _dbconn.BeginTransaction();
				_dbadapter.UpdateCommand.Transaction = _trans;

				_dbadapter.UpdateCommand.Parameters.Clear();
				_dbadapter.UpdateCommand.Parameters.Add(new SqlParameter("@Company_Id", SqlDbType.VarChar, 20));
				_dbadapter.UpdateCommand.Parameters.Add(new SqlParameter("@CompanyCode", SqlDbType.VarChar, 250));
				_dbadapter.UpdateCommand.Parameters.Add(new SqlParameter("@Company", SqlDbType.NVarChar, 250));
				_dbadapter.UpdateCommand.Parameters.Add(new SqlParameter("@Address", SqlDbType.NVarChar, 250));
				_dbadapter.UpdateCommand.Parameters.Add(new SqlParameter("@Tel", SqlDbType.VarChar, 50));
				_dbadapter.UpdateCommand.Parameters.Add(new SqlParameter("@Fax", SqlDbType.VarChar, 50));
				_dbadapter.UpdateCommand.Parameters.Add(new SqlParameter("@WebSite", SqlDbType.VarChar, 250));
				_dbadapter.UpdateCommand.Parameters.Add(new SqlParameter("@Email", SqlDbType.VarChar, 50));
				_dbadapter.UpdateCommand.Parameters.Add(new SqlParameter("@Tax", SqlDbType.VarChar, 50));
				_dbadapter.UpdateCommand.Parameters.Add(new SqlParameter("@Licence", SqlDbType.NVarChar, 50));
				_dbadapter.UpdateCommand.Parameters.Add(new SqlParameter("@Photo", SqlDbType.Image));
				_dbadapter.UpdateCommand.Parameters.Add(new SqlParameter("@BankAccount", SqlDbType.VarChar, 20));
				_dbadapter.UpdateCommand.Parameters.Add(new SqlParameter("@OpenedAt", SqlDbType.NVarChar, 255));
				_dbadapter.UpdateCommand.Prepare();
				SqlString Company_IdSQL = Company_Id;
				_dbadapter.UpdateCommand.Parameters["@Company_Id"].Value = Company_IdSQL;
				SqlString CompanyCodeSQL = CompanyCode;
				_dbadapter.UpdateCommand.Parameters["@CompanyCode"].Value = CompanyCodeSQL;
				SqlString CompanySQL = Company;
				_dbadapter.UpdateCommand.Parameters["@Company"].Value = CompanySQL;
				SqlString AddressSQL = Address;
				_dbadapter.UpdateCommand.Parameters["@Address"].Value = AddressSQL;
				SqlString TelSQL = Tel;
				_dbadapter.UpdateCommand.Parameters["@Tel"].Value = TelSQL;
				SqlString FaxSQL = Fax;
				_dbadapter.UpdateCommand.Parameters["@Fax"].Value = FaxSQL;
				SqlString WebSiteSQL = WebSite;
				_dbadapter.UpdateCommand.Parameters["@WebSite"].Value = WebSiteSQL;
				SqlString EmailSQL = Email;
				_dbadapter.UpdateCommand.Parameters["@Email"].Value = EmailSQL;
				SqlString TaxSQL = Tax;
				_dbadapter.UpdateCommand.Parameters["@Tax"].Value = TaxSQL;
				SqlString LicenceSQL = Licence;
				_dbadapter.UpdateCommand.Parameters["@Licence"].Value = LicenceSQL;
				SqlBinary PhotoSQL = Convert.FromBase64String(Photo);
				_dbadapter.UpdateCommand.Parameters["@Photo"].Value = PhotoSQL;
				SqlString BankAccountSQL = BankAccount;
				_dbadapter.UpdateCommand.Parameters["@BankAccount"].Value = BankAccountSQL;
				SqlString OpenedAtSQL = OpenedAt;
				_dbadapter.UpdateCommand.Parameters["@OpenedAt"].Value = OpenedAtSQL;
				Int32 returnvalue = Convert.ToInt32(_dbadapter.UpdateCommand.ExecuteScalar());

				_trans.Commit();
				return returnvalue;
			}
			catch (SqlException e)
			{
				try
				{
					_trans.Rollback();
				}
				catch (SqlException ex)
				{
					throw new Exception("SQL Rollback Error: SYS_COMPANY_Update: " + ex.Message, ex);
				}
				throw new Exception("SQL Error: SYS_COMPANY_Update: " + e.Message, e);
			}
			finally
			{
				if (_dbadapter.UpdateCommand != null)
					_dbadapter.UpdateCommand.Dispose();
				CloseConnection();
			}
		}
		public Int32 SYS_COMPANY_Disable(String Company_Id, ref Boolean Enabled)
		{
			try
			{
				if (_dbconn.State != ConnectionState.Open)
					_dbconn.Open();
				_dbadapter.UpdateCommand = new SqlCommand("SYS_COMPANY_Disable");
				_dbadapter.UpdateCommand.CommandType = CommandType.StoredProcedure;
				_dbadapter.UpdateCommand.Connection = _dbconn;

				_trans = _dbconn.BeginTransaction();
				_dbadapter.UpdateCommand.Transaction = _trans;

				_dbadapter.UpdateCommand.Parameters.Clear();
				_dbadapter.UpdateCommand.Parameters.Add(new SqlParameter("@Company_Id", SqlDbType.VarChar, 20));
				_dbadapter.UpdateCommand.Parameters.Add(new SqlParameter("@enabled", SqlDbType.Bit));
				_dbadapter.UpdateCommand.Prepare();
				SqlString Company_IdSQL = Company_Id;
				_dbadapter.UpdateCommand.Parameters["@Company_Id"].Value = Company_IdSQL;
				_dbadapter.UpdateCommand.Parameters["@enabled"].Direction = ParameterDirection.Output;
				Int32 returnvalue = Convert.ToInt32(_dbadapter.UpdateCommand.ExecuteScalar());

				_trans.Commit();
				return returnvalue;
			}
			catch (SqlException e)
			{
				try
				{
					_trans.Rollback();
				}
				catch (SqlException ex)
				{
					throw new Exception("SQL Rollback Error: SYS_COMPANY_Disable: " + ex.Message, ex);
				}
				throw new Exception("SQL Error: SYS_COMPANY_Disable: " + e.Message, e);
			}
			finally
			{
				if (_dbadapter.UpdateCommand != null)
					_dbadapter.UpdateCommand.Dispose();
				CloseConnection();
			}
		}
		public Int32 SYS_COMPANY_Delete(String Company_Id)
		{
			try
			{
				if (_dbconn.State != ConnectionState.Open)
					_dbconn.Open();
				_dbadapter.DeleteCommand = new SqlCommand("SYS_COMPANY_Delete");
				_dbadapter.DeleteCommand.CommandType = CommandType.StoredProcedure;
				_dbadapter.DeleteCommand.Connection = _dbconn;

				_trans = _dbconn.BeginTransaction();
				_dbadapter.DeleteCommand.Transaction = _trans;

				_dbadapter.DeleteCommand.Parameters.Clear();
				_dbadapter.DeleteCommand.Parameters.Add(new SqlParameter("@Company_Id", SqlDbType.VarChar, 20));
				_dbadapter.DeleteCommand.Prepare();
				SqlString Company_IdSQL = Company_Id;
				_dbadapter.DeleteCommand.Parameters["@Company_Id"].Value = Company_IdSQL;
				Int32 returnvalue = Convert.ToInt32(_dbadapter.DeleteCommand.ExecuteScalar());

				_trans.Commit();
				return returnvalue;
			}
			catch (SqlException e)
			{
				try
				{
					_trans.Rollback();
				}
				catch (SqlException ex)
				{
					throw new Exception("SQL Rollback Error: SYS_COMPANY_Delete: " + ex.Message, ex);
				}
				throw new Exception("SQL Error: SYS_COMPANY_Delete: " + e.Message, e);
			}
			finally
			{
				if (_dbadapter.DeleteCommand != null)
					_dbadapter.DeleteCommand.Dispose();
				CloseConnection();
			}
		}
		public Int32 SYS_COMPANY_Save(String Company_Id, String CompanyCode, String Company, String Address, String Tel, String Fax, String WebSite, String Email, String Tax, String Licence, String Photo, String BankAccount, String OpenedAt)
		{
			try
			{
				if (_dbconn.State != ConnectionState.Open)
					_dbconn.Open();
				_dbadapter.UpdateCommand = new SqlCommand("SYS_COMPANY_Save");
				_dbadapter.UpdateCommand.CommandType = CommandType.StoredProcedure;
				_dbadapter.UpdateCommand.Connection = _dbconn;

				_trans = _dbconn.BeginTransaction();
				_dbadapter.UpdateCommand.Transaction = _trans;

				_dbadapter.UpdateCommand.Parameters.Clear();
				_dbadapter.UpdateCommand.Parameters.Add(new SqlParameter("@Company_Id", SqlDbType.VarChar, 20));
				_dbadapter.UpdateCommand.Parameters.Add(new SqlParameter("@CompanyCode", SqlDbType.VarChar, 250));
				_dbadapter.UpdateCommand.Parameters.Add(new SqlParameter("@Company", SqlDbType.NVarChar, 250));
				_dbadapter.UpdateCommand.Parameters.Add(new SqlParameter("@Address", SqlDbType.NVarChar, 250));
				_dbadapter.UpdateCommand.Parameters.Add(new SqlParameter("@Tel", SqlDbType.VarChar, 50));
				_dbadapter.UpdateCommand.Parameters.Add(new SqlParameter("@Fax", SqlDbType.VarChar, 50));
				_dbadapter.UpdateCommand.Parameters.Add(new SqlParameter("@WebSite", SqlDbType.VarChar, 250));
				_dbadapter.UpdateCommand.Parameters.Add(new SqlParameter("@Email", SqlDbType.VarChar, 50));
				_dbadapter.UpdateCommand.Parameters.Add(new SqlParameter("@Tax", SqlDbType.VarChar, 50));
				_dbadapter.UpdateCommand.Parameters.Add(new SqlParameter("@Licence", SqlDbType.NVarChar, 50));
				_dbadapter.UpdateCommand.Parameters.Add(new SqlParameter("@Photo", SqlDbType.Image));
				_dbadapter.UpdateCommand.Parameters.Add(new SqlParameter("@BankAccount", SqlDbType.VarChar, 20));
				_dbadapter.UpdateCommand.Parameters.Add(new SqlParameter("@OpenedAt", SqlDbType.NVarChar, 255));
				_dbadapter.UpdateCommand.Prepare();
				SqlString Company_IdSQL = Company_Id;
				_dbadapter.UpdateCommand.Parameters["@Company_Id"].Value = Company_IdSQL;
				SqlString CompanyCodeSQL = CompanyCode;
				_dbadapter.UpdateCommand.Parameters["@CompanyCode"].Value = CompanyCodeSQL;
				SqlString CompanySQL = Company;
				_dbadapter.UpdateCommand.Parameters["@Company"].Value = CompanySQL;
				SqlString AddressSQL = Address;
				_dbadapter.UpdateCommand.Parameters["@Address"].Value = AddressSQL;
				SqlString TelSQL = Tel;
				_dbadapter.UpdateCommand.Parameters["@Tel"].Value = TelSQL;
				SqlString FaxSQL = Fax;
				_dbadapter.UpdateCommand.Parameters["@Fax"].Value = FaxSQL;
				SqlString WebSiteSQL = WebSite;
				_dbadapter.UpdateCommand.Parameters["@WebSite"].Value = WebSiteSQL;
				SqlString EmailSQL = Email;
				_dbadapter.UpdateCommand.Parameters["@Email"].Value = EmailSQL;
				SqlString TaxSQL = Tax;
				_dbadapter.UpdateCommand.Parameters["@Tax"].Value = TaxSQL;
				SqlString LicenceSQL = Licence;
				_dbadapter.UpdateCommand.Parameters["@Licence"].Value = LicenceSQL;
				SqlBinary PhotoSQL = Convert.FromBase64String(Photo);
				_dbadapter.UpdateCommand.Parameters["@Photo"].Value = PhotoSQL;
				SqlString BankAccountSQL = BankAccount;
				_dbadapter.UpdateCommand.Parameters["@BankAccount"].Value = BankAccountSQL;
				SqlString OpenedAtSQL = OpenedAt;
				_dbadapter.UpdateCommand.Parameters["@OpenedAt"].Value = OpenedAtSQL;
				Int32 returnvalue = Convert.ToInt32(_dbadapter.UpdateCommand.ExecuteScalar());

				_trans.Commit();
				return returnvalue;
			}
			catch (SqlException e)
			{
				try
				{
					_trans.Rollback();
				}
				catch (SqlException ex)
				{
					throw new Exception("SQL Rollback Error: SYS_COMPANY_Save: " + ex.Message, ex);
				}
				throw new Exception("SQL Error: SYS_COMPANY_Save: " + e.Message, e);
			}
			finally
			{
				if (_dbadapter.UpdateCommand != null)
					_dbadapter.UpdateCommand.Dispose();
				CloseConnection();
			}
		}
		public DataSet SYS_COMPANY_GetList()
		{
			try
			{
				if (_dbconn.State != ConnectionState.Open)
					_dbconn.Open();
				_dbadapter.SelectCommand = new SqlCommand("SYS_COMPANY_GetList");
				_dbadapter.SelectCommand.CommandType = CommandType.StoredProcedure;
				_dbadapter.SelectCommand.Connection = _dbconn;
				_dbadapter.SelectCommand.Parameters.Clear();
				_dbadapter.SelectCommand.Prepare();
				_dbadapter.Fill(_data);

				return _data;
			}
			catch (SqlException e)
			{
				throw new Exception("SQL Error: SYS_COMPANY_GetList: " + e.Message, e);
			}
			finally
			{
				if (_dbadapter.SelectCommand != null)
					_dbadapter.SelectCommand.Dispose();
				CloseConnection();
			}
		}
		public DataSet SYS_COMPANY_Get(String Company_Id)
		{
			try
			{
				if (_dbconn.State != ConnectionState.Open)
					_dbconn.Open();
				_dbadapter.SelectCommand = new SqlCommand("SYS_COMPANY_Get");
				_dbadapter.SelectCommand.CommandType = CommandType.StoredProcedure;
				_dbadapter.SelectCommand.Connection = _dbconn;
				_dbadapter.SelectCommand.Parameters.Clear();
				_dbadapter.SelectCommand.Parameters.Add(new SqlParameter("@Company_Id", SqlDbType.VarChar, 20));
				_dbadapter.SelectCommand.Prepare();
				SqlString Company_IdSQL = Company_Id;
				_dbadapter.SelectCommand.Parameters["@Company_Id"].Value = Company_IdSQL;
				_dbadapter.Fill(_data);

				return _data;
			}
			catch (SqlException e)
			{
				throw new Exception("SQL Error: SYS_COMPANY_Get: " + e.Message, e);
			}
			finally
			{
				if (_dbadapter.SelectCommand != null)
					_dbadapter.SelectCommand.Dispose();
				CloseConnection();
			}
		}
	}
}
